//
//  ButtonCell.swift
//  MyCloudMusic
//
//  Created by jin on 2024/8/28.
//

import UIKit

//提供类似Android中更高层级布局框架
import TangramKit

class ButtonCell:BaseTableViewCell{
    
    static let NAME:String = "ButtonCell"
    
    var buttonClick:((ButtonData)->Void)!
    
    override func initViews() {
        super.initViews()
        
        container.addSubview(scrollView)
    }
    
    func bind(_ data:ButtonData) {
        if contentContainer.subviews.count > 0{
            return
        }
        
        let containerWidth=(UIScreen.main.bounds.width-10*2)/4.5
        for (index,it) in data.datum.enumerated(){
            let buttonView = ButtonView()
            buttonView.tg_width.equal(containerWidth)
            buttonView.bind(it.title, it.icon)
            buttonView.tag = 200 + index
            
            if it.title == R.string.localizable.dayRecommend(){
                //显示日期
                buttonView.tipView.text = "\(SuperDateUtil.currentDay())"
            }
            
            //设置点击事件
            buttonView.addTapGestureRecognizer(target: self, action: #selector(buttonViewClick(_:)))
            contentContainer.addSubview(buttonView)
        }
    }
    
    @objc func buttonViewClick(_ uuu:UITapGestureRecognizer) {
        
        let button = uuu.view as! ButtonView
        
        print("buttonViewClick \(button.titleView.text) tag \(button.tag)")
    }
    
    lazy var scrollView: UIScrollView = {
        let r = UIScrollView()
        r.contentInset = UIEdgeInsets(top: 0, left: 13, bottom: 0, right: 13)
        r.showsHorizontalScrollIndicator = false
        r.tg_width.equal(.fill)
        r.tg_height.equal(100)
        
        //真实内容容器
        r.addSubview(contentContainer)
        
        return r
    }()
    
    lazy var contentContainer: TGLinearLayout = {
        let r = TGLinearLayout(.horz)
        r.tg_width.equal(.wrap)
        r.tg_height.equal(100)
        r.tg_gravity = TGGravity.vert.center
        return r
    }()
}
